﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fcom.VNInsurance.Web.Models;

namespace Fcom.VNInsurance.Web.Controllers
{
     [Authorize(Roles = "13,14")]
    public class CarManagerController : Controller
    {
        //
        // GET: /CarManager/

        Fcom.VNInsurance.Web.Models.VNInsuranceDataContext db = new Models.VNInsuranceDataContext();
        [Authorize(Roles = "13,14")]
        public ActionResult Index()
        {
            return View();
        }
        [Authorize(Roles = "13,14")]
        [HttpGet]
        public ActionResult AddCar()
        {
            ViewBag.AutoCategory=(from a in db.AutoCategories select a) ;
            return View();
        }
        [HttpPost]
        public ActionResult AddCar(Car car)
        {
            Car c = new Car();
            var isexit = db.Cars.Where(m => m.Name == car.Name).FirstOrDefault();
            if (isexit == null)
            {
                c.AutoCatID = car.AutoCatID;
                c.Name = car.Name;
                db.Cars.InsertOnSubmit(c);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
       [Authorize(Roles = "13,14")]
        public JsonResult listcar()
        {
            var autocatlist = (from a in db.Cars
                               select new
                               {
                                   CarID = a.ID,
                                   Name = a.Name,
                                   AutoName=a.AutoCategory.Name
                                   ,a.SeatNumber,a.Year,a.Image
                               });
          
            return Json(autocatlist, JsonRequestBehavior.AllowGet);
        }
        [Authorize(Roles = "13,14")]
        [HttpGet]
        public ActionResult EditCar(int id)
        {
            var car = db.Cars.Where(m => m.ID == id).FirstOrDefault();
            ViewBag.AutoCategory = (from a in db.AutoCategories select a);
            try
            {
                return View(car);

            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
        [HttpPost]
        public ActionResult EditCar(Car car)
        {
            var foud = db.Cars.Where(m => m.ID == car.ID).FirstOrDefault();
            if (foud != null)
            {
                foud.Name = car.Name;
                foud.AutoCatID = car.AutoCatID;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        [HttpPost]
        public ActionResult removeauto(int id)
        {
            var at = db.Cars.Where(m => m.ID == id).FirstOrDefault();
            db.Cars.DeleteOnSubmit(at);
            db.SubmitChanges();
            return Json(true);
        }

    }
}
